package Main;

import java.util.*;

import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.servlet.*;

import Corpus.*;
import DataObjects.*;
import Servlets.*;
import SolR.*;
import Tagger.*;

public class Active {
	public static void active(Corpus corpus,Tagger tagger,String url){
		try {
		corpus.init();
		
		HashMap<Integer,Topic> allTopics=tagger.tagCorpus(corpus.getArticlesPath());
			
//		HashMap<Integer,Topic> allTopics=topicsReader();
//		Vector<Topic> topics=TopicsReader.topicsReader("out\\keys.txt");//TODO
		MySolR solR=new MySolR(url,allTopics,corpus);
		System.out.println(11);//TODO
		solR.init(corpus,tagger);//TODO at list one time in day
		System.out.println(22);//TODO
		
		Server server = new Server(Consts.SERVER_PORT);
		ServletContextHandler ctx =
			new ServletContextHandler(ServletContextHandler.SESSIONS);

		ctx.setContextPath("/");

		ctx.addServlet(new ServletHolder(new SearchServlet(corpus)), "/search");
		ctx.addServlet(new ServletHolder(new SearchResultServlet(solR,allTopics)), "/result");//TODO do samthing abot the topics
		ctx.addServlet(new ServletHolder(new ArticleServlet(corpus)), "/article");
		ctx.addServlet(new ServletHolder(new ArticleResultServlet(solR)), "/article2");
		ctx.addServlet(new ServletHolder(new HTMLSenderServlet("enterQuery",corpus)), "/main");
		ctx.addServlet(new ServletHolder(new HTMLSenderServlet("wz_jsgraphics.js")), "/wz_jsgraphics.js");

		// Files
		ResourceHandler res = new ResourceHandler();
		res.setResourceBase(".");
		HandlerList list = new HandlerList();
		list.addHandler(ctx);
		list.addHandler(res);
		server.setHandler(list);			

		server.start();
		server.join();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
}
